home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1099 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  84 lines

  1. Newsgroups: comp.lang.c
  2. Path: tank.news.pipex.net!pipex!warwick!bsmail!talisker!nathan
  3. From: nathan@pact.srf.ac.uk (Nathan Sidwell)
  4. Subject: Re: Override a cpp macro?
  5. Message-ID: <DL0onF.BF8@uns.bris.ac.uk>
  6. Sender: usenet@uns.bris.ac.uk (Usenet news owner)
  7. Nntp-Posting-Host: talisker.pact.srf.ac.uk
  8. Organization: Inmos
  9. X-Newsreader: TIN [version 1.2 PL2]
  10. References: <4d0v5r$6fs@oak78.doc.ic.ac.uk>
  11. Date: Thu, 11 Jan 1996 12:42:51 GMT
  12.  
  13. Richard Jones (rwmj@doc.ic.ac.uk) wrote:
  14. : I'm trying to redefine a cpp macro, but using the old definition. I've got
  15. : a macro defined:
  16.  
  17. :     #define m1(a,b) /* some hidden definition of m1 */
  18. :[tried things like]
  19. :     #define old_m1(a,b) m1(a,b)
  20. :     #undef m1
  21. :     #define m1(a,b) (old_m1(a,b)+1)
  22.  
  23. : But neither works as expected. I've tried variations using the macro:
  24. you're using the wrong value of expected!
  25.  
  26. : What's the way to do this? Surely this is an obvious thing to want to do,
  27. : so there must be some simple point I'm missing?
  28. No, you cannot do what you want with cpp. #define macros are simple 
  29. text substitution (with arguments). what #define is saying is
  30. take the rest of the line (+continuations with \) and remember it. No
  31. interpretation happens at this point. Then when the #define name is
  32. encountered (outside of a #define), the name (and args) are replaced
  33. with the previously remembered text. (Note to pedants, I've ignored
  34. the concept of tokens here.) The substututed text is then rescanned
  35. for further #define substitutions.
  36.  
  37. Substitutions in the body of the #define happen when the #define
  38. is being used, not when it is being defined.
  39.  
  40. so if I have
  41. #define  BUFFER 3
  42. #define  BUFFER2 (BUFFER*2)
  43.  
  44. the text for BUFFER2 is '(BUFFER*2)', it is not '(3*2)'.
  45.  
  46. This does make it possible to have
  47.  
  48.     #define colour pink
  49.     #define pink red
  50.     colour
  51.  
  52. expand to
  53.     red
  54.  
  55. rather than pink, which is what would happen if body expansion happened at
  56. definition. So the order of #defines is not important.
  57.  
  58. :     #define expand(a) a
  59.  
  60. : which according to the Gnu info page for (Gnu) cpp should expand the
  61. : argument first. However, no known combination works.
  62.  
  63. What this is saying is that when a #define has arguments, they are expanded
  64. before substitution into the macro body, not after. So
  65.  
  66.     #define FOO(arg) before arg after
  67.     #define OTHER    other_val
  68.     FOO(OTHER)
  69.  
  70. proceeds via the intermediate stage
  71.     FOO(other_val)
  72. rather than the stage
  73.     before OTHER after
  74. which would have been the case if arguments are expanded after
  75. substitution. (Things are different if the # or ## operators are used)
  76.  
  77. nathan
  78. --
  79. Nathan Sidwell                         Holder of the Xmris home page
  80. Chameleon Architecture Group at SGS-Thomson, formerly Inmos
  81. http://www.pact.srf.ac.uk/~nathan/                  Tel 0117 9707182
  82. nathan@inmos.co.uk or nathan@bristol.st.com or nathan@pact.srf.ac.uk
  83. Having problems?     try http://www.pact.srf.ac.uk/~nathan/problems/
  84.